package com.bweab.dao.impl;

import com.bweab.dao.api.DepositTbDaoInterface;
import com.bweab.dao.base.BaseDao;
import com.bweab.pojo.DepositTb;

import java.util.List;

/**
 * 充值记录查询DAO
 * */
public class DepositTbDaoImpl extends BaseDao<DepositTb> implements DepositTbDaoInterface {
    /**
     *
     * @param userid 用户id
     * @param usermonkey 用户金钱
     * @param dormitoryid 寝室id
     * @return 返回影响结果
     * 添加充值记录
     */


    @Override
    public int insertDeposit(int userid, int usermonkey, int dormitoryid) {
        //实现数据的更新
        //插值的时候不带外键
        //准备sql语句
        String sql ="insert into deposit_tb (user_id,dormitory_id,deposit_monkey) values (\n" +
                "                               ?,\n" +
                "                               ?,\n" +
                "                               ?\n" +
                "                              )";
        int row1 = alter(sql,userid,dormitoryid,usermonkey);
        //更新数据
        String updateSql = "update dormitory_tb set dormitory_monkey = dormitory_monkey+? where dormitory_id=?;";
        int row2 =alter(updateSql, usermonkey,dormitoryid);
        System.out.println(row1+"\n"+row2+"影响结果");
        if (row1>0 && row2>0){
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public DepositTb getDepositInfoById(int depositId) {

        //准备sql
        String sql="select deposit_tb.deposit_id depositId,\n" +
                "       deposit_tb.user_id userId,\n" +
                "       deposit_tb.dormitory_id dormitoryId,\n" +
                "       deposit_tb.deposit_monkey depositMonkey,\n" +
                "       deposit_tb.deposit_time depositTime\n" +
                "\n" +
                "from deposit_tb\n" +
                "\n" +
                "where deposit_id=?";


        //获取充值信息
        DepositTb depositInfo = querySingle(sql,DepositTb.class,depositId);

        return depositInfo;
    }
    /**
     * 该方法修改充值记录的
     *
     * @param dormitoryId 寝室id
     * @param userId      用户id
     * @param userFee     用户费用
     * @param depositId
     * @return 1成功 0失败
     */
    @Override
    public int alterDepositInfo(int dormitoryId, int userId, int userFee, int depositId) {
       //准备sql

       String sql="update deposit_tb set\n" +
               "                      user_id=?,\n" +
               "                      dormitory_id=?,\n" +
               "                      deposit_monkey=?\n" +
               "\n" +
               "where deposit_id=?;";

       //调用baseDao
        int rows= alter(sql,userId,dormitoryId,userFee,depositId);


        return rows;
    }


    /**
     * 通过寝室id删除充值记录
     * @param dormitoryId 寝室id
     * @return 1成功 0失败
     */
    @Override
    public int deleteDepositByDorrmitoryId(int dormitoryId) {
        //准备sql
        String sql="delete from deposit_tb\n" +
                "\n" +
                "where dormitory_id=?;";

        int rows= alter(sql,dormitoryId);
        return rows;
    }


    /***
     * 通过充值记录id删除
     * @param depositId 充值记录id
     * @return 1成功 0 失败
     */
    @Override
    public int deleteDepositByDepositId(int depositId) {
        String sql="delete from deposit_tb\n" +
                "\n" +
                "where deposit_id=?;";
        int rows=alter(sql,depositId);
        return rows;
    }

    /**
     * 通过用户id删除结果
     * @param userId 用户id
     * @return 0失败
     */
    @Override
    public int deleteDepositByUserId(int userId) {
        String sql="delete from deposit_tb\n" +
                "\n" +
                "where user_id=?;";
        int rows=alter(sql,userId);
        return rows;
    }

    /**
     * 通过用户id查询所以的deposit
     * @param depositId 充值记录id
     * @return 对象集合
     */
    @Override
    public List<DepositTb> queryDepositInfoByUserId(int depositId) {
        //查询所有
        //准备sql语句查询
        String  sql="";

        List<DepositTb> depositTbList=queryList(sql,DepositTb.class,depositId);
        return depositTbList;
    }

    /**
     *
     * @param depositId 充值记录id
     * @param page      当前页
     * @param pageSize  limit 获取大小
     * @return 返回结果是一个对象集合
     */
    @Override
    public List<DepositTb> queryDepositInfoByUserId(int depositId, int page, int pageSize) {
        //对页数进行判断
        int start;
        //准备sql
        String sql="select\n" +
                "    deposit_tb.deposit_id depositId,\n" +
                "    deposit_tb.user_id userId,\n" +
                "    deposit_tb.dormitory_id dormitoryId,\n" +
                "    deposit_tb.deposit_monkey depositMonkey,\n" +
                "    deposit_tb.deposit_time depositTime\n" +
                "\n" +
                "\n" +
                "from deposit_tb\n" +
                "where deposit_id=?\n" +
                "limit ?,?\n" +
                "\n" +
                ";";
        if(page==0){
            start=0;
        }else {
            start=pageSize*(page-1);
        }

        List<DepositTb> depositTbList = queryList(sql, DepositTb.class, depositId, start, pageSize);

        return depositTbList;
    }

    //查询记录查询实现
}
